//
//  BLLineChatView.m
//  Byelone
//
//  Created by 乔小乔 on 2021/3/2.
//

#import "BLLineChatView.h"
@interface BLLineChatView ()
@property (nonatomic, assign) CGFloat xWidth;//横向单位值
@property (nonatomic, assign) CGFloat yWidth;//纵向单位值
@property (nonatomic, strong) NSMutableArray  *pointsArray;//坐标值数组
@property (nonatomic, strong) NSMutableArray *startArray;
@property (nonatomic, strong) NSMutableArray *bezierArray;//曲线的数组
@end
@implementation BLLineChatView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
    }
    return self;
}

- (void)drawRect:(CGRect)rect{
    
}
- (void)loadView{
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    for (CAShapeLayer *layer in self.bezierArray) {
        [layer removeFromSuperlayer];
    }
    self.startArray = [NSMutableArray new];
    self.pointsArray = @[].mutableCopy;
    self.xWidth = (CGRectGetWidth(self.frame)-self.itemWidth) / (self.arrayBjColor.count-1);
    self.yWidth = (CGRectGetHeight(self.frame)-self.textHeight*2) / self.yTotal;
    [self createBj];
    [self drawBezierPath];
}
//背景柱形图
- (void)createBj{
    CGFloat space = (CGRectGetWidth(self.frame)-self.itemWidth*self.arrayBjColor.count)/(self.arrayBjColor.count-1);
    for (int i=0; i<self.arrayBjColor.count; i++) {
        BLChatModel *model = self.arrayBjColor[i];
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake((self.itemWidth+space)*i, self.textHeight, self.itemWidth, CGRectGetHeight(self.frame)-self.textHeight*2)];
        
        view.backgroundColor = model.color?:[UIColor lightGrayColor];
        view.layer.cornerRadius = 12;
        view.layer.masksToBounds = YES;
        [self addSubview:view];
        
        UILabel *labelTop = [[UILabel alloc]initWithFrame:CGRectMake((self.itemWidth+space)*i, 0, 0, 22)];
        UILabel *labelBottom = [[UILabel alloc]initWithFrame:CGRectMake((self.itemWidth+space)*i, CGRectGetHeight(self.frame)-self.textHeight, 0, self.textHeight)];
        labelTop.font = [UIFont systemFontOfSize:self.textFont];
        labelBottom.font = [UIFont systemFontOfSize:self.textFont];
        labelTop.textColor = self.textColor?:[UIColor lightGrayColor];
        labelBottom.textColor = self.textColor?:[UIColor lightGrayColor];
        labelTop.text = model.topTitle;
        labelBottom.text = model.bottomTitle;
        
        [labelTop sizeToFit];
        [labelBottom sizeToFit];
        labelTop.center = CGPointMake(view.center.x, self.textHeight*0.5);
        labelBottom.center = CGPointMake(view.center.x, CGRectGetHeight(self.frame)-self.textHeight*0.5);
        
        [self addSubview:labelTop];
        [self addSubview:labelBottom];
    }
}
//画点
- (void)createPoint:(CGPoint)point witnColor:(UIColor *)color{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(point.x-3.25, point.y-3.25, 6.5, 6.5)];
    view.layer.cornerRadius = 3.25;
    view.layer.borderColor = [UIColor whiteColor].CGColor;
    view.layer.borderWidth = 1;
    view.backgroundColor = color;
    [self addSubview:view];
}

//线图
- (void)drawBezierPath{
    for (int i=0; i<self.pointYArray.count; i++) {
        [self.startArray removeAllObjects];
        [self.pointsArray removeAllObjects];
        
        UIColor *bjColor = self.arrayLineColor[i];
        NSArray *arrayPoint = self.pointYArray[i];
        
        [arrayPoint enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSInteger objInter = 0;
            if ([obj respondsToSelector:@selector(integerValue)]) {
                objInter = [obj integerValue];
                objInter = objInter+self.differenceValue;
                if (objInter < 0) {
                    objInter = 0;
                } else if (objInter > self.yTotal) {
                    objInter = self.yTotal;
                }
            }
            
            CGPoint point = CGPointMake(self.xWidth * idx + self.itemWidth*0.5, CGRectGetHeight(self.frame) - objInter * self.yWidth-self.textHeight);
            NSValue *value = [NSValue valueWithCGPoint:CGPointMake(point.x, point.y)];
            [self.pointsArray addObject:value];
            if (idx == 0 || idx == arrayPoint.count-1) {
                [self.startArray addObject:value];
            }
        }];
        //    添加一个初始点
        NSValue *firstPointValue = [NSValue valueWithCGPoint:CGPointMake(self.itemWidth*0.5-self.xWidth, CGRectGetHeight(self.frame) / 2)];
        [self.pointsArray insertObject:firstPointValue atIndex:0];
        //    添加一个终点
        NSValue *endPointValue = [NSValue valueWithCGPoint:CGPointMake(CGRectGetWidth(self.frame)-self.itemWidth*0.5+self.xWidth, CGRectGetHeight(self.frame)/2)];
        [self.pointsArray addObject:endPointValue];
        
        /** 计算折线路径 */
        UIBezierPath *path = [UIBezierPath bezierPath];
        for (NSInteger i = 0; i < arrayPoint.count-1; i++) {
            CGPoint p1 = [[self.pointsArray objectAtIndex:i] CGPointValue];
            CGPoint p2 = [[self.pointsArray objectAtIndex:i+1] CGPointValue];
            CGPoint p3 = [[self.pointsArray objectAtIndex:i+2] CGPointValue];
            CGPoint p4 = [[self.pointsArray objectAtIndex:i+3] CGPointValue];
            if (i == 0) {
                [path moveToPoint:p2];
            }
            [self getControlPointx0:p1.x andy0:p1.y x1:p2.x andy1:p2.y x2:p3.x andy2:p3.y x3:p4.x andy3:p4.y path:path];
        }
        
        //            折线的白色背景，一条更粗的线假装是背景
        CAShapeLayer *_bezierBjLayer = [CAShapeLayer layer];
        _bezierBjLayer.path = path.CGPath;
        _bezierBjLayer.strokeColor = [UIColor whiteColor].CGColor;
        _bezierBjLayer.fillColor = [[UIColor clearColor] CGColor];
        // 默认设置路径宽度为0，使其在起始状态下不显示
        _bezierBjLayer.lineWidth = self.lineWidth+2;
        _bezierBjLayer.lineCap = kCALineCapRound;
        _bezierBjLayer.lineJoin = kCALineJoinRound;
        [self.layer addSublayer:_bezierBjLayer];
        
        /** 将折线添加到折线图层上，并设置相关的属性 */
        CAShapeLayer *_bezierLineLayer = [CAShapeLayer layer];
        _bezierLineLayer.path = path.CGPath;
        _bezierLineLayer.strokeColor = bjColor.CGColor;
        _bezierLineLayer.fillColor = [[UIColor clearColor] CGColor];
        // 默认设置路径宽度为0，使其在起始状态下不显示
        _bezierLineLayer.lineWidth = self.lineWidth;
        _bezierLineLayer.lineCap = kCALineCapRound;
        _bezierLineLayer.lineJoin = kCALineJoinRound;
        [self.layer addSublayer:_bezierLineLayer];
        [self.bezierArray addObject:_bezierBjLayer];
        [self.bezierArray addObject:_bezierLineLayer];
        
        for (NSValue *pointV in self.startArray) {
            CGPoint p = pointV.CGPointValue;
            [self createPoint:p witnColor:bjColor];
        }
    }
    
}
//根据每条线当前起点和终点，以及更前和更后两个点，一共四个点，求出各个控制点
- (void)getControlPointx0:(CGFloat)x0 andy0:(CGFloat)y0
                       x1:(CGFloat)x1 andy1:(CGFloat)y1
                       x2:(CGFloat)x2 andy2:(CGFloat)y2
                       x3:(CGFloat)x3 andy3:(CGFloat)y3
                     path:(UIBezierPath*) path{
    CGFloat smooth_value =0.6;
    CGFloat ctrl1_x;
    CGFloat ctrl1_y;
    CGFloat ctrl2_x;
    CGFloat ctrl2_y;
    CGFloat xc1 = (x0 + x1) /2.0;
    CGFloat yc1 = (y0 + y1) /2.0;
    CGFloat xc2 = (x1 + x2) /2.0;
    CGFloat yc2 = (y1 + y2) /2.0;
    CGFloat xc3 = (x2 + x3) /2.0;
    CGFloat yc3 = (y2 + y3) /2.0;
    CGFloat len1 = sqrt((x1-x0) * (x1-x0) + (y1-y0) * (y1-y0));
    CGFloat len2 = sqrt((x2-x1) * (x2-x1) + (y2-y1) * (y2-y1));
    CGFloat len3 = sqrt((x3-x2) * (x3-x2) + (y3-y2) * (y3-y2));
    CGFloat k1 = len1 / (len1 + len2);
    CGFloat k2 = len2 / (len2 + len3);
    CGFloat xm1 = xc1 + (xc2 - xc1) * k1;
    CGFloat ym1 = yc1 + (yc2 - yc1) * k1;
    CGFloat xm2 = xc2 + (xc3 - xc2) * k2;
    CGFloat ym2 = yc2 + (yc3 - yc2) * k2;
    ctrl1_x = xm1 + (xc2 - xm1) * smooth_value + x1 - xm1;
    ctrl1_y = ym1 + (yc2 - ym1) * smooth_value + y1 - ym1;
    ctrl2_x = xm2 + (xc2 - xm2) * smooth_value + x2 - xm2;
    ctrl2_y = ym2 + (yc2 - ym2) * smooth_value + y2 - ym2;
    [path addCurveToPoint:CGPointMake(x2, y2) controlPoint1:CGPointMake(ctrl1_x, ctrl1_y) controlPoint2:CGPointMake(ctrl2_x, ctrl2_y)];
}

@end

@implementation BLChatModel
@end
